<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 4.1</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="4.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="4.2.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P1">Part I. The Language</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#4">Chapter 4. Statements</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><a name="assignment"><h2>4.1 &ndash; Assignment</h2></a>

<p>

<p>Assignment is the basic means of changing the value of
a variable or a table field:
<pre>
    a = "hello" .. "world"
    t.n = t.n + 1
</pre>

<p>Lua allows <em>multiple assignment</em>,
where a list of values is assigned to a list of variables in one step.
Both lists have their elements separated by commas.
For instance, in the assignment
<pre>
    a, b = 10, 2*x
</pre>
the variable <code>a</code> gets the value 10 and <code>b</code> gets <code>2*x</code>.

<p>In a multiple assignment,
Lua first evaluates all values
and only then executes the assignments.
Therefore,
we can use a multiple assignment to swap two values,
as in
<pre>
    x, y = y, x                -- swap `x' for `y'
    a[i], a[j] = a[j], a[i]    -- swap `a[i]' for `a[j]'
</pre>

<p>Lua always <em>adjusts</em>
the number of values to the number of variables:
When the list of values is shorter than the list of variables,
the extra variables receive <B>nil</B> as their values;
when the list of values is longer,
the extra values are silently discarded:
<pre>
    a, b, c = 0, 1
    print(a,b,c)           --> 0   1   nil
    a, b = a+1, b+1, b+2   -- value of b+2 is ignored
    print(a,b)             --> 1   2
    a, b, c = 0
    print(a,b,c)           --> 0   nil   nil
</pre>
The last assignment in the above example shows a common mistake.
To initialize a set of variables,
you must provide a value for each one:
<pre>
    a, b, c = 0, 0, 0
    print(a,b,c)           --> 0   0   0
</pre>

<p>Actually, most of the previous examples are somewhat artificial.
I seldom use multiple assignment simply to write several assignments
in one line.
But often we really need multiple assignment.
We already saw an example, to swap two values.
A more frequent use is to collect multiple returns from function calls.
As we will discuss in detail later,
a function call can return multiple values.
In such cases, a single expression can supply the values
for several variables.
For instance, in the assignment
<pre>
    a, b = f()
</pre>
<code>f()</code> returns two results:
<code>a</code> gets the first and <code>b</code> gets the second.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="4.2.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

